﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;
using System.Configuration;

namespace ATW.MT.CommonClass
{
    public static class common
    {

        /// <summary>
        /// 过滤请求参数中潜在的xss代码
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool filterRequestString(string str)
        {
            if (str.Trim() != "")
            {
                string[] pattern = { "<", "%3c", ">", "%3e", "script", "document", "eval", "/", "%2f", "//" };
                string _s = str.Trim().Replace(" ", "");
                for (int i = 0; i < pattern.Length; i++)
                {
                    Regex r = new Regex(pattern[i]);
                    if (r.IsMatch(str)) return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 过滤sql注入字符串 true通过 false失败
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool filterChar(string str)
        {
            if (str.Trim() != "")
            {
                string[] pattern = { "select", "insert", "delete", "from", "count\\(", "drop table", "update", "truncate", "asc\\(", 
                                   "mid\\(", "char\\(", "xp_cmdshell", "exec   master", "netlocalgroup administrators", "net user",
                                   " or ", " and " };
                for (int i = 0; i < pattern.Length; i++)
                {
                    Regex r = new Regex(pattern[i]);
                    if (r.IsMatch(str)) return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 过滤sql注入字符串 true通过 false失败
        /// </summary>
        /// <param name="str"></param>
        /// <param name="maxLength"></param>
        /// <returns></returns>
        public static bool filterChar(string str, int maxLength)
        {
            if (str.Trim() != "")
            {
                if (str.Length > maxLength) return false;
                string[] pattern = { "select", "insert", "delete", "from", "count\\(", "drop table", "update", "truncate", "asc\\(", 
                                   "mid\\(", "char\\(", "xp_cmdshell", "exec   master", "netlocalgroup administrators", "net user",
                                   "or", "and" };
                for (int i = 0; i < pattern.Length; i++)
                {
                    Regex r = new Regex(pattern[i]);
                    if (r.IsMatch(str)) return false;
                }
            }
            return true;
        }
        /// <summary>
        /// 是否手机号
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool isMobile(string s)
        {
            if (s.Length != 11) return false;
            return Regex.IsMatch(s, @"^[1]+[3,5,8]+\d{9}");
        }
        /// <summary>
        /// 是否整数
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool isInt(string s)
        {
            if (!String.IsNullOrEmpty(s)) {
                return Regex.IsMatch(s,@"^\d+$");
            }
            return true;
        }
        /// <summary>
        /// 是否浮点型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool isFloat(string s)
        {
            if (!string.IsNullOrEmpty(s))
            {
                return Regex.IsMatch(s, @"^\d+[.]{0,1}\d+$");
            }
            return true;
        }
        /// <summary>
        /// 至今为止的时间
        /// </summary>
        /// <param name="start"></param>
        /// <param name="format">单位，dd 天,hh 小时,mm 分钟</param>
        /// <returns></returns>
        public static int TimeTillNow(DateTime start, string format)
        {
            if (format == "dd")
                return (int)((TimeSpan)DateTime.Now.Subtract(start)).Days;
            if (format == "hh")
                return (int)((TimeSpan)DateTime.Now.Subtract(start)).Hours;
            if (format == "mm")
                return (int)((TimeSpan)DateTime.Now.Subtract(start)).Minutes;
            return 0;
        }

        public static string GetUrl()
        {
            string strUrl = "";
            strUrl = ConfigurationManager.AppSettings["GetFilePath"];

            return strUrl;
        }

    }
}